package com.charlotte;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.UnknownHostException;

/**
 *
 * @author law
 */
public class Peticion 
{
    
    protected HttpURLConnection conect;
    
    protected Peticion(String url)throws IOException
    {
        conect = (HttpURLConnection)new URL(url).openConnection();
        conect.setDoInput(true);
        conect.setConnectTimeout(20000);
        conect.setReadTimeout(20000);
    }
    
    public String get()throws FileNotFoundException, IOException, UnknownHostException, SocketTimeoutException          
    {
        StringWriter result;
        this.conect.connect();
        try(BufferedReader reader = new BufferedReader(new InputStreamReader(conect.getInputStream())))
        {
            result = new StringWriter();
            char[] c = new char[5];
            int x;
            while((x = reader.read(c)) != -1)
                result.write(c,0,x);
        }
        this.conect.disconnect();
        return result != null ? new String(result.getBuffer()) : null;
    }
    
}
